package mylist
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MediaEvent;
	import flash.media.CameraUI;
	import flash.media.MediaPromise;
	import flash.media.MediaType;
	import flash.system.Capabilities;
	import flash.utils.ByteArray;
	import flash.utils.IDataInput;
	
	public class AndroidCamera extends Sprite
	{
		public static var main:AndroidCamera;
		private var cameraUI:CameraUI;
		private var dataSource:IDataInput;
		//private var eventSource:IEventDispatcher;
		
		public function AndroidCamera()
		{
			if (main)
				return;
			
            logs.adds("avHardwareDisable: " + Capabilities.avHardwareDisable);
            logs.adds("hasAccessibility: " + Capabilities.hasAccessibility);
            logs.adds("hasAudio: " + Capabilities.hasAudio);
            logs.adds("hasAudioEncoder: " + Capabilities.hasAudioEncoder);
            logs.adds("hasEmbeddedVideo: " + Capabilities.hasEmbeddedVideo);
            logs.adds("hasIME: " + Capabilities.hasIME);
            logs.adds("hasMP3: " + Capabilities.hasMP3);
            logs.adds("hasPrinting: " + Capabilities.hasPrinting);
            logs.adds("hasScreenBroadcast: " + Capabilities.hasScreenBroadcast);
            logs.adds("hasScreenPlayback: " + Capabilities.hasScreenPlayback);
            logs.adds("hasStreamingAudio: " + Capabilities.hasStreamingAudio);
            logs.adds("hasStreamingVideo: " + Capabilities.hasStreamingVideo);
            logs.adds("hasTLS: " + Capabilities.hasTLS);
            logs.adds("hasVideoEncoder: " + Capabilities.hasVideoEncoder);
            logs.adds("isDebugger: " + Capabilities.isDebugger);
            logs.adds("isEmbeddedInAcrobat: " + Capabilities.isEmbeddedInAcrobat);
            logs.adds("language: " + Capabilities.language);
            logs.adds("localFileReadDisable: " + Capabilities.localFileReadDisable);
            logs.adds("manufacturer: " + Capabilities.manufacturer);
            logs.adds("maxLevelIDC: " + Capabilities.maxLevelIDC);
            logs.adds("os: " + Capabilities.os);
            logs.adds("pixelAspectRatio: " + Capabilities.pixelAspectRatio);
            logs.adds("playerType: " + Capabilities.playerType);
            logs.adds("screenColor: " + Capabilities.screenColor);
            logs.adds("screenDPI: " + Capabilities.screenDPI);
            logs.adds("screenResolutionX: " + Capabilities.screenResolutionX);
            logs.adds("screenResolutionY: " + Capabilities.screenResolutionY);
            logs.adds("serverString: " + Capabilities.serverString);
            logs.adds("touchscreenType: " + Capabilities.touchscreenType);
            
            // The following capabilities are supported only when publishing for AIR.
            // If you are publishing for AIR, uncomment the following lines.   
			logs.adds("-----air------");
            logs.adds("cpuArchitecture: " + Capabilities.cpuArchitecture);
            logs.adds("languages: " + Capabilities.languages);
            logs.adds("supports32BitProcesses: " + Capabilities.supports32BitProcesses);
            logs.adds("supports64BitProcesses: " + Capabilities.supports64BitProcesses);        
                        
            logs.adds("version: " + Capabilities.version);
			
			imageBytes = null;
			main = this;
			addEventListener(Event.ADDED_TO_STAGE, init);
			//init();
		}
		
		public function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			if (CameraUI.isSupported)
			{
				cameraUI = new CameraUI();
				logs.adds("Initializing camera...");
				cameraUI.addEventListener(MediaEvent.COMPLETE, imageSelected);
				//cameraUI.addEventListener(Event.CANCEL, canceled);
				cameraUI.addEventListener(ErrorEvent.ERROR, mediaError);
				cameraUI.launch(MediaType.IMAGE);
			}
			else
			{
				logs.adds("CameraUI is not supported.");
			}
		}
		
		private function removes():void
		{
			cameraUI.removeEventListener(MediaEvent.COMPLETE, imageSelected);
			//cameraUI.removeEventListener(Event.CANCEL, canceled);
			cameraUI.removeEventListener(ErrorEvent.ERROR, mediaError);
		}
		
		private function imageSelected(event:MediaEvent):void
		{
			logs.adds("Media selected...");
			var imagePromise:MediaPromise = event.data;
			
			if (imagePromise.file != null && false)
			{
				FileItem.push(imagePromise.file);
				new Loaders();
				removes();
			}
			else
			{
				dataSource = imagePromise.open();
				if (imagePromise.isAsync)
				{
					logs.adds("Asynchronous media promise.");
					var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
					eventSource.addEventListener(Event.COMPLETE, onMediaLoaded);
				}
				else
				{
					logs.adds("Synchronous media promise.");
					readMediaData();
				}
			}
		}
		
		private function onMediaLoaded(e:Event):void
		{
			IEventDispatcher(e.target).removeEventListener(Event.COMPLETE, onMediaLoaded);
			logs.adds("Media load complete");
			readMediaData();
		}
		
		//private function canceled(event:Event):void
		//{
			//logs.adds("Media select canceled");
			//cameraUI.launch(MediaType.IMAGE); //Upload another image
		//}
		
		public function get Bmp():ByteArray
		{
			return imageBytes;
		}
		public function setNull():void
		{
			imageBytes = null;
		}
		private var imageBytes:ByteArray;
		private function readMediaData():void
		{
			imageBytes = new ByteArray();
			dataSource.readBytes(imageBytes);
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, addPhoto);
			try
			{
				loader.loadBytes(imageBytes);
			}
			catch (e:Error)
			{
				logs.adds(e);
				cameraUI.launch(MediaType.IMAGE);
			}
			removes();
		}
		
		private function addPhoto(e:Event):void
		{
			logs.adds(e);
			e.target.removeEventListener(Event.COMPLETE, addPhoto);
			ViewList.main.addChilds(e.target.content);
			
			cameraUI = null;
			dataSource = null;
		}
		
		private function mediaError(error:ErrorEvent):void
		{
			logs.adds("Error:" + error.text);
			cameraUI.launch(MediaType.IMAGE);
		}
	
	}
}